﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace Webcam
{
    static class Program
    {
        /// <summary>
        /// The program's window
        /// </summary>
        public static Form1 form;

        /// <summary>
        /// The webcams in use by the system
        /// </summary>
        public static List<VideoSource.Webcam> webcams = new List<VideoSource.Webcam>();

        public static int Scan = 5;
        public static int Diff = 100;
        public static double FPS = 1;
        public static int LineDiff = 50;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            webcams.Add(new VideoSource.Webcam(0));
            webcams.Add(new VideoSource.Webcam(1));
            form = new Form1();

            foreach (VideoSource.Webcam w in webcams)
            {
                Thread clientThread = new Thread(new ParameterizedThreadStart(ProcessFrame));
                clientThread.Name = "Processes webcam data and displays it";
                clientThread.IsBackground = true;
                clientThread.Start(w);

                w.Start();
            }
            Application.Run(form);
        }

        /// <summary>
        /// Process a frame
        /// </summary>
        /// <param name="w"></param>
        static void ProcessFrame(object obj)
        {
            VideoSource.Webcam w = (VideoSource.Webcam)obj;
            DateTime previous = DateTime.Now;
            while (true)
            {
                if (w.DeviceNumber == 0)
                {
                    TimeSpan diff = DateTime.Now - previous;
                    previous = DateTime.Now;
                    FPS = 1000.0 / diff.TotalMilliseconds;
                }
                VideoSource.Frame f = w.GetFrame();
                if (f == null) // no frames held in buffer
                {
                    Thread.Sleep(100);
                    continue;
                }

                System.Drawing.Bitmap b = f.frame;
                Image.RGBImage src = new Image.RGBImage(b);
                Image.RGBImage img = src.Clone();
                img.ToGrayScale();

                Image.EdgeDetection ed = new Webcam.Image.EdgeDetection(img);
                ed.Process();

                Image.RGBImage output = src.Clone();
                Image.GraphicsDrawing.DrawLine(output, ed.Lines);

                //Image.GraphicsDrawing.DrawGrid(etout, et.BoxSize, et.BoxSize);

                form.UpdateWebcamPic(w.DeviceNumber + 1, output.ToBitmap());
                
            }
        }
        

        static void w_Capture_Changed(object sender, EventArgs e)
        {
            VideoSource.Webcam w = (VideoSource.Webcam)sender;

            
        }
    }
}
